package alex.java;

import java.lang.reflect.Field;
import java.util.Objects;

public class ReflectObjectProperties {

    public static void main(String[] args) {
        CertificatesInputValue value = new CertificatesInputValue();

        System.out.println("==========1==========");
        System.out.println(allEntityFieldIsNUll(value));
        System.out.println(allEntityFieldIsFull(value));
        System.out.println(allEntityFieldHasValue(value));


        System.out.println("==========2==========");
        value.setCode("");
        System.out.println(allEntityFieldIsNUll(value));
        System.out.println(allEntityFieldIsFull(value));
        System.out.println(allEntityFieldHasValue(value));


        System.out.println("==========3==========");
        value.setCode("230123123");
        value.setExpiryDate("2015466");
        value.setValue("123");
        System.out.println(allEntityFieldIsNUll(value));
        System.out.println(allEntityFieldIsFull(value));
        System.out.println(allEntityFieldHasValue(value));
    }


    /**
     * 根据属性名设置属性值
     *
     * @param fieldName
     * @param object
     * @return
     */
    private static void setFieldValueByFieldName(String fieldName, Object object, String value) {
        try {
            // 获取obj类的字节文件对象
            Class c = object.getClass();
            // 获取该类的成员变量
            Field f = c.getDeclaredField(fieldName);
            // 取消语言访问检查
            f.setAccessible(true);
            // 给变量赋值
            f.set(object, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 校验此对象, 所有属性是否为空
     *
     * @param o
     * @return true: 都为空; false: 不都为空
     */
    private static boolean allEntityFieldIsNUll(Object o) {
        try {
            for (Field field : o.getClass().getDeclaredFields()) {
                // 把私有属性公有化
                field.setAccessible(true);
                Object object = field.get(o);
                if (!Objects.isNull(object)) {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        return true;
    }

    /**
     * 校验此对象, 所有属性是否都有值
     *
     * @param o
     * @return true: 都有值; false: 不都有值
     */
    private static boolean allEntityFieldIsFull(Object o) {
        try {
            for (Field field : o.getClass().getDeclaredFields()) {
                // 把私有属性公有化
                field.setAccessible(true);
                Object object = field.get(o);
                if (Objects.isNull(object)) {
                    return false;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

        return true;
    }

    /**
     * 校验此对象, 所有属性中, 是否存在有值的属性
     *
     * @param o
     * @return true: 存在; false: 不存在
     */
    private static boolean allEntityFieldHasValue(Object o) {
        try {
            for (Field field : o.getClass().getDeclaredFields()) {
                // 把私有属性公有化
                field.setAccessible(true);
                Object object = field.get(o);
                if (Objects.nonNull(object)) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }

        return false;
    }

    public static class CertificatesInputValue {
        String code;//证件类型编码
        String value;//证件编码
        String expiryDate;//有效期

        public CertificatesInputValue(String code, String value, String expiryDate) {
            this.code = code;
            this.value = value;
            this.expiryDate = expiryDate;
        }

        public CertificatesInputValue() {
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public String getExpiryDate() {
            return expiryDate;
        }

        public void setExpiryDate(String expiryDate) {
            this.expiryDate = expiryDate;
        }

        @Override
        public String toString() {
            return "CertificatesInputValue{" +
                    "code='" + code + '\'' +
                    ", value='" + value + '\'' +
                    ", expiryDate='" + expiryDate + '\'' +
                    '}';
        }
    }
}
